package top.sxlai.controller;

import top.sxlai.constant.LoginStatus;
import top.sxlai.entity.DTO.LoginRequest;
import top.sxlai.service.OpUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : sxl
 * @date : 2024/4/6 16:07
 * @Version: 1.0
 */

@RestController
@Slf4j
public class LoginController {
    @Autowired
    private OpUserService userLogin;
    @PostMapping("/login")
    public ResponseEntity<Map<String,String>> login(@RequestBody LoginRequest loginRequest){
        //获取工厂角色
//        LoginFactory loginFactory = new LoginFactory();
        String username = loginRequest.getUsername();
        int roleId = Integer.parseInt(loginRequest.getRoleId());
        log.info("用户名：{} 开始登录... ", username);
        // 假设根据用户名和密码查询到了用户信息，并获取到了用户的角色ID
        if (roleId==1){
            //roleId==1用户登录
//          ILogin userLogin = loginFactory.getUserLogin();
          return userLogin.login(loginRequest);
        } else if (roleId==2) {
            //roleId==2管理元登录
//            ILogin managerLogin = loginFactory.getManagerLogin();
//            return managerLogin.login(username, password);
        }
        Map<String,String> response = new HashMap<>();
        response.put("message", LoginStatus.FAILURE.getMessage());
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
    }

}
